#include "ucosiifile.h"
extern "C"
{
#include "yaffsfs.h"
}




EclrFile* EclrFile::Open(const char* fname , enum OpenFlags mode)
{
    int result=-1;
/*
enum OpenFlags
{ 
    Open_r = 1,     // open for reading only
    Open_w = 2,     // open for writing only
    Open_rw = 3,    // open for reading and writing
    Open_c = 4      // create file (if existing, it will be deleted and created new
}
*/
    switch(mode)
    {
    case Open_r:
        result = yaffs_open(fname,O_RDONLY, S_IREAD);
//        result = FS_FOpen(fname,"rb");
        break;
    case Open_w:
        result = yaffs_open(fname,O_WRONLY| O_CREAT, S_IWRITE);

//        result = FS_FOpen(fname, "wb");
        break;
    case Open_rw:
        result = yaffs_open(fname,O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
//        result = FS_FOpen(fname,"r+b");
        break;
    case Open_c:
        result = yaffs_open(fname,O_RDWR | O_CREAT, S_IREAD | S_IWRITE);
//        result = FS_FOpen(fname,"w+b");
        break;
    default:
        break;
    }
	if(result!=-1)result+=1;
	else result = NULL;


//debugout("@@@@@@@@file open:%s %x %x\r\n",fname,mode,result);
    return (EclrFile*)result;
}


bool EclrFile::Remove(const char* fname)
{
	bool result = false;

	if(yaffs_unlink(fname)!=-1)
    {
        result = true;
    }
//debugout("@@@@@@@@file remove:%s %d\r\n",fname,result);
    return result;
}
bool EclrFile::Rename(const char* pSrcName, const char* pDstName)
{
	bool result = false;

	if(yaffs_rename(pSrcName,pDstName)!=-1)
    {
        result = true;
    }
//debugout("@@@@@@@@file rename:%s @ %s %d\r\n",pSrcName,pDstName,result);
    return result;
}
bool EclrFile::Close(EclrFile* pFile)
{
    bool result = false;

    if (pFile != NULL)
    {
        yaffs_close((int)pFile-1);
        result = true;
    }
//debugout("@@@@@@@@file close %d\r\n",(int)pFile);
    return result;
}

int EclrFile::Read(EclrFile* pFile, void* buffer, int size)
{
//    int nRVal = FS_FRead(buffer, 1, size, (FS_FILE*)pFile);
	int nRVal = yaffs_read((int)pFile-1,buffer,size);

//debugout("@@@@@@@@file read %d %d\r\n",nRVal,size);
    return nRVal;
}

int EclrFile::Write(EclrFile* pFile, void* buffer, int size)
{
//    int nRVal = FS_FWrite(buffer, 1, size, (FS_FILE*)pFile);
	int nRVal = yaffs_write((int)pFile-1,buffer,size);
	if(nRVal==-1)nRVal=0;
//debugout("*********file write %d %d\r\n",nRVal,size);
    return nRVal;
}

long EclrFile::Seek(EclrFile* pFile, long offset, enum SeekModes seekMode)
{
    int newSeekMode = 0;
    long retVal = (long)0;

    switch(seekMode)
    {
    case Seek_set:
        newSeekMode =(int)SEEK_SET;
        break;
    case Seek_cur:
        newSeekMode = (int)SEEK_CUR;
        break;
    case Seek_end:
        newSeekMode = (int)SEEK_END;
        break;
    default:
        retVal = (long)-1;
        break;
    }
//debugout("@@@@@@@@file seek%x %x\r\n",offset,newSeekMode);
    if(retVal != (long)-1)
    {
        retVal=yaffs_lseek((int)pFile-1, offset, newSeekMode);
    }
    return retVal;
}
